package com.skyinno.mrms.redis;

import java.lang.reflect.Method;

import org.apache.poi.ss.formula.functions.T;

import com.skyinno.mrms.utils.Config;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {

	// 连接池
	private static JedisPool pool;

	private static RedisUtil redisUtil;
	
	
	public static String getClassName(Class c) {
		return c.getName().replaceAll("com.sky.entity.", "");
	}
	public String getObjectId(Object object){
		try {
			Method m =object.getClass().getMethod("getId", null);
			String id = m.invoke(object, null).toString();
			return id;
		} catch (Exception e) {
			return "";
		}
	}

	static{
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(Integer.parseInt(Config.getProperty("redis.maxIdle")));
		// config.setMinIdle(50);
		config.setMaxTotal(Integer.parseInt(Config.getProperty("redis.maxTotal")));
		config.setTestOnBorrow(true);
		String redisAddress = Config.getProperty("redis.address");
		int redisPort = Integer.parseInt(Config.getProperty("redis.port"));
		int redisTimeout = Integer.parseInt(Config.getProperty("redis.timout"));
		String password = Config.getProperty("redis.password");
		pool = new JedisPool(config, redisAddress, redisPort, redisTimeout, password);
	}
	
	public static RedisUtil getInstance(){
		if(redisUtil==null){
			redisUtil = new RedisUtil();
		}
		return redisUtil;
	}
	
	private RedisUtil() {
	}
	
	public static Jedis getResource(){
		return pool.getResource();
	}
	/**
	 * 删除数据
	 * @param id
	 */
	public void delete(String id){
		Jedis jedis = pool.getResource();
		try {
			jedis.del(id);
		} catch (Exception e) {
			
		}finally{
			jedis.close();
		}
	}

	/**
	 * 删除数据
	 * 
	 * @param object
	 */
	public void deleteObjectr(Object object)  {
		if (object != null) {
			Jedis jedis = pool.getResource();
			try {
				jedis.hdel("model." + getClassName(object.getClass()), getObjectId(object));
			} catch (Exception e) {
				
			}finally{
				jedis.close();
			}
		}
	}

	/**
	 * 新增或修改数据
	 * 
	 * @param object
	 */
	public void saveOrUpdateObject(T object)  {
		if (object != null) {
			Jedis jedis = pool.getResource();
			try {
				String value = SerializableUtil.serialize(object);
				jedis.hset("model." + getClassName(object.getClass()), getObjectId(object), value);
			} catch (Exception e) {
				
			}finally{
				jedis.close();
			}
		}
	}
	
	/**
	 * 写入字符串数据
	 * @param key
	 * @param value
	 */
	public void setStringValue(String key,String value){
		Jedis jedis = pool.getResource();
		try {
			jedis.set(key, value);
		} catch (Exception e) {
			
		}finally{
			jedis.close();
		}
	}
	/**
	 * 获取字符串数据
	 * @param key
	 * @return
	 */
	public String getStringValue(String key){
		String value = "";
		Jedis jedis = pool.getResource();
		try {
			value = jedis.get(key);
		} catch (Exception e) {
			
		}finally{
			jedis.close();
		}
		return value;
	}
	/**
	 * 删除字符串数据
	 * @param key
	 */
	public void deleteStringValue(String key){
		Jedis jedis = pool.getResource();
		try {
			jedis.del(key);
		} catch (Exception e) {
			
		}finally{
			jedis.close();
		}
	}
	
	public void deleteHashValue(String key,String field){
		Jedis jedis = pool.getResource();
		try {
			jedis.hdel(key, field);
		} catch (Exception e) {
			
		}finally{
			jedis.close();
		}
	}
}
